package com.qixin.modules.eaApplication.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qixin.modules.ea.entity.EaEntertainReceptionActivity;
import com.qixin.modules.ea.entity.EaPhoneRecord;
import com.qixin.modules.ea.service.IEaEntertainReceptionActivityService;
import com.qixin.modules.ea.service.IEaPhoneRecordService;
import com.qixin.modules.ea.vo.ListPageVo;
import com.qixin.modules.eaApplication.entity.EaEntertainApply;
import com.qixin.modules.eaApplication.entity.EaHostObject;
import com.qixin.modules.eaApplication.mapper.EaEntertainApplyMapper;
import com.qixin.modules.eaApplication.service.IEaEntertainApplyService;
import com.qixin.modules.eaApplication.service.IEaEntertainApplyServiceCopy;
import com.qixin.modules.eaApplication.service.IEaHostObjectService;
import com.qixin.modules.eaApplication.vo.EaEntertainApplyPage;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import com.qixin.modules.eaBasic.vo.IdVo;
import com.qixin.modules.eaBasic.vo.UserIdPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.EmptyUtil;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.nc.entity.NcPersonnel;
import org.jeecg.modules.nc.service.INcPersonnelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
* @Description: 招待费申请单
* @Author: qixin-boot
* @Date:   2021-07-06
* @Version: V1.0
*/
@Api(tags="app招待费申请单")
@RestController
@RequestMapping("/app/api/eaEntertainApply")
@Slf4j
public class ApiEaEntertainApplyController {
    @Autowired
    private IEaEntertainApplyService eaEntertainApplyService;
    @Autowired
    private EaEntertainApplyMapper eaEntertainApplyMapper;
    @Autowired
    private INcPersonnelService ncPersonnelService;
    @Autowired
    private IEaHostObjectService eaHostObjectService;
    @Autowired
    private IEaPhoneRecordService eaPhoneRecordService;
    @Autowired
    private IEaEntertainReceptionActivityService eaEntertainReceptionActivityService;

    /**
     *   添加
     *
     * @param eaEntertainApplyPage
     * @return
     */
    @AutoLog(value = "招待费申请单-添加")
    @ApiOperation(value="招待费申请单-添加", notes="招待费申请单-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody EaEntertainApplyPage eaEntertainApplyPage) {
        EaEntertainApply eaEntertainApply = new EaEntertainApply();
        BeanUtils.copyProperties(eaEntertainApplyPage, eaEntertainApply);

        // update-begin-Autor: wjb date: 2022-02-09 10:47 for: 生成单据编号
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("prefix","ZDSQ"); // 编码前缀
        Object result = FillRuleUtil.executeRule("common_code", jsonObj);
        eaEntertainApply.setBillNo(result.toString());
        // update-end-Autor: wjb date: 2022-02-09 10:47 for: 生成单据编号
        eaEntertainApply.setState(0);
        eaEntertainApplyService.saveMain(eaEntertainApply, eaEntertainApplyPage.getEaHostObjectList(),eaEntertainApplyPage.getEaEntertainReceptionActivities(),eaEntertainApplyPage.getEaPhoneRecord());
        //add-begin-by 2022-02-07 wjb for 返回主表、子表数据给前端
        EaEntertainApplyPage resultPage = new EaEntertainApplyPage();
        BeanUtils.copyProperties(eaEntertainApply,resultPage);
        resultPage.setEaHostObjectList(eaEntertainApplyPage.getEaHostObjectList());
        resultPage.setEaEntertainReceptionActivities(eaEntertainApplyPage.getEaEntertainReceptionActivities());
        resultPage.setEaPhoneRecord(eaEntertainApplyPage.getEaPhoneRecord());
        //add-end-by 2022-02-07 wjb for 返回主表、子表数据给前端
        return Result.OK(resultPage);
    }

//    /**
//     * 编辑招待费申请信息
//     *
//     * @param eaEntertainApplyPage
//     * @return
//     */
//    @AutoLog(value = "招待费申请信息-编辑")
//    @ApiOperation(value="招待费申请信息-编辑", notes="招待费申请信息-编辑")
//    @PostMapping(value = "/edit")
//    public Result<?> edit(@RequestBody EaEntertainApplyPage eaEntertainApplyPage) {
//        EaEntertainApply eaEntertainApply = new EaEntertainApply();
//        BeanUtils.copyProperties(eaEntertainApplyPage, eaEntertainApply);
//        EaEntertainApply eaEntertainApplyEntity = eaEntertainApplyService.getById(eaEntertainApply.getId());
//        if(eaEntertainApplyEntity==null) {
//            return Result.error("未找到对应数据");
//        }
//
//        //判断单据是否已经被提交
//        EaEntertainApply eaEntertainApply1 = eaEntertainApplyService.getById(eaEntertainApplyPage.getId());
//        String status = EmptyUtil.checkNull(eaEntertainApply1.getState());   //当状态为1审批中,2已审批时,提示单据状态已变更,请刷新
//        if(status.equals("1") || status.equals("2")){
//            return Result.error("单据状态已变更,请刷新");
//        }
//
//        eaEntertainApply.setSaveType("add");    //保存
//        Map<String,Object> returnMap = eaEntertainApplyService.updateMain(eaEntertainApply,eaEntertainApplyPage.getEaHostObjectList(),eaEntertainApplyPage.getEaEntertainReceptionActivities(), eaEntertainApplyPage.getEaPhoneRecord());
//
//        if(returnMap.get("code").equals("-1")){
//            return Result.error(EmptyUtil.checkNull(returnMap.get("message")));
//        }else{
//            returnMap.remove("code");
//            returnMap.remove("message");
//            return Result.OK(returnMap);
//        }
//    }

    /**
     *  编辑
     *
     * @param eaEntertainApplyPage
     * @return
     */
    @AutoLog(value = "招待费申请单-编辑")
    @ApiOperation(value="招待费申请单-编辑", notes="招待费申请单-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody EaEntertainApplyPage eaEntertainApplyPage) {
        EaEntertainApply eaEntertainApply = new EaEntertainApply();
        BeanUtils.copyProperties(eaEntertainApplyPage, eaEntertainApply);
        EaEntertainApply eaEntertainApplyEntity = eaEntertainApplyService.getById(eaEntertainApply.getId());
        if(eaEntertainApplyEntity==null) {
            return Result.error("未找到对应数据");
        }
        eaEntertainApply.setSaveType("add");    //保存
        eaEntertainApplyService.updateMain(eaEntertainApply, eaEntertainApplyPage.getEaHostObjectList(),eaEntertainApplyPage.getEaEntertainReceptionActivities(),eaEntertainApplyPage.getEaPhoneRecord());
        //add-begin-by 2022-02-07 wjb for 返回主表、子表数据给前端
        EaEntertainApplyPage eaEntertainApplyPage1 = new EaEntertainApplyPage();
        BeanUtils.copyProperties(eaEntertainApply,eaEntertainApplyPage1);
        eaEntertainApplyPage1.setEaHostObjectList(eaEntertainApplyPage.getEaHostObjectList());
        eaEntertainApplyPage1.setEaEntertainReceptionActivities(eaEntertainApplyPage.getEaEntertainReceptionActivities());
        eaEntertainApplyPage1.setEaPhoneRecord(eaEntertainApplyPage.getEaPhoneRecord());
        //add-end-by 2022-02-07 wjb for 返回主表、子表数据给前端
        return Result.OK(eaEntertainApplyPage1);
    }

//    /**
//     * 查询招待费申请信息列表
//     *
//     * @param listPageVo
//     * @return Result
//     */
//    @ApiOperation(value="招待费申请信息列表-查询", notes="招待费申请信息列表-查询")
//    @PostMapping(value = "/list")
//    public Result<?> list(@RequestBody ListPageVo listPageVo) {
//        Map<String,Object> resutlList = eaEntertainApplyService.list(listPageVo);
//        return Result.OK(resutlList);
//    }

    /**
    * @Description
    * @author  wujinbiao
    * @date 2022/2/17
    * @param[ pageNo, pageSize, keyword, userid, req]
    * @return org.jeecg.common.api.vo.Result<?>
    */
    @ApiOperation(value="app招待费申请单-关联报销单查询列表", notes="招待费申请单-关联报销单查询列表")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   @RequestParam(name="keyword", required = false) String keyword,
                                   @RequestParam(name="userid", required = false) String userid,
                                   HttpServletRequest req) {
        QueryWrapper<EaEntertainApply> param = new QueryWrapper<>();
        //update-begin-by wjb 2022-02-17 16：22 for 添加查询条件
        param.orderByDesc("create_time");
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 当前用户创建，并且单据状态为审批通过，未报销
        param.eq("create_by",loginUser.getId()).eq("state",2);
        param.and(
                wrapper -> wrapper.isNull("ea_entertain_id").or().eq("ea_entertain_id","")
        );
        // 判断是否帮代理人报销，如果是 userid有值
        if (StringUtils.isNotBlank(userid)){
            param.eq("person_id", userid);
        }
        if(StringUtils.isNotBlank(keyword)){
            param.and(
              p-> p.like("bill_no",keyword).or().like("cause",keyword)
            );

        }
        //update-end-by wjb 2022-02-17 16：22 for 添加查询条件

        Page<EaEntertainApply> page = new Page<EaEntertainApply>(pageNo, pageSize);
        IPage<EaEntertainApply> pageList = eaEntertainApplyService.page(page, param);
        //add-begin-by wujinbiao for 详情返回申请人名称
        List<EaEntertainApply> records = pageList.getRecords();
        for (EaEntertainApply eaEntertainApply1: records) {
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaEntertainApply1.getPersonId());
            if(ncPersonnel != null){
                eaEntertainApply1.setPersonIdName(ncPersonnel.getName());
            }
        }
        //add-end-by wujinbiao for 详情返回申请人名称
        return Result.OK(pageList);
    }

//    /**
//     * 查询招待费申请详情
//     *
//     * @param idVo
//     * @return
//     */
//    @ApiOperation(value="招待费申请详情-查询", notes="招待费申请详情-查询")
//    @PostMapping(value = "/selectById")
//    public Result<?> selectById(@RequestBody IdVo idVo) {
//        String id = idVo.getId();
//        EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectById(id);
//        if(eaEntertainApply==null) {
//            return Result.error("未找到对应数据");
//        }
//        Map<String, Object> resutlList = eaEntertainApplyService.selectById(id);
//        return Result.OK(resutlList);
//    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="招待费申请单-通过id查询", notes="招待费申请单-通过id查询")
    @GetMapping(value = "/selectById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        EaEntertainApplyPage eaEntertainApplyPage = new EaEntertainApplyPage();
        EaEntertainApply eaEntertainApply = eaEntertainApplyService.getById(id);
        if(eaEntertainApply==null) {
            return Result.error("未找到对应数据");
        }
        //add-begin-by wjb for 详情返回申请人名称、子表数据
        if(StringUtils.isNotBlank(eaEntertainApply.getPersonId())){
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaEntertainApply.getPersonId());
            eaEntertainApply.setPersonIdName(ncPersonnel.getName());
        }
        BeanUtils.copyProperties(eaEntertainApply,eaEntertainApplyPage);
        //招待对象子表
        List<EaHostObject> eaHostObjects = eaHostObjectService.selectByMainId(eaEntertainApply.getId());
        List<EaEntertainReceptionActivity> eaEntertainReceptionActivities = eaEntertainReceptionActivityService.selectByMainId(eaEntertainApply.getId());
        List<EaPhoneRecord> eaPhoneRecords = eaPhoneRecordService.selectByMainId(eaEntertainApply.getId());
        eaEntertainApplyPage.setEaHostObjectList(eaHostObjects);
        eaEntertainApplyPage.setEaEntertainReceptionActivities(eaEntertainReceptionActivities);
        eaEntertainApplyPage.setEaPhoneRecord(eaPhoneRecords.get(0));
        //add-end-by wjb for 详情返回申请人名称、子表数据
        return Result.OK(eaEntertainApplyPage);
    }

//    /**
//     * 通过id删除招待费申请
//     *
//     * @param idVo
//     * @return
//     */
//    @AutoLog(value = "招待费申请信息-通过id删除")
//    @ApiOperation(value="招待费申请信息-通过id删除", notes="招待费申请信息-通过id删除")
//    @PostMapping(value = "/delete")
//    public Result<?> delete(@RequestBody IdVo idVo) {
//        String id = idVo.getId();
//        Map<String,Object> returnMap = eaEntertainApplyService.delMain(id);
//        if(returnMap.get("code").equals("0")){
//            return Result.OK("删除成功!");
//        }else{
//            String message = returnMap.get("message") != null ? returnMap.get("message").toString() : "";
//            return Result.error(message);
//        }
//    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "招待费申请单-通过id删除")
    @ApiOperation(value="招待费申请单-通过id删除", notes="招待费申请单-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        QueryWrapper<EaEntertainApply> params = new QueryWrapper<>();
        params.eq("id",id);
        EaEntertainApply eaEntertainApply = eaEntertainApplyService.getOne(params);
        if (eaEntertainApply != null && eaEntertainApply.getState()>0){
            return Result.error("该单据已发起流程,不可删除!");
        }
        eaEntertainApplyService.delMain(id);
        return Result.OK("删除成功!");
    }

//    /**
//     * 招待费申请审批/退回
//     *
//     * @param eaTaskVo
//     * @return
//     */
//    @AutoLog(value = "招待费申请审批/退回")
//    @ApiOperation(value="招待费申请审批/退回", notes="招待费申请审批/退回")
//    @PostMapping(value = "/audit")
//    public Result<?> edit(@RequestBody EaTaskVo eaTaskVo) {
//        Map<String,Object> returnMap = eaEntertainApplyService.audit(eaTaskVo);
//        return Result.OK(returnMap);
//    }

}
